package com.df.tank.designPattern.singleton;

/**
 * 懒汉式
 * 使用synchronized保证线程安全
 * 尽可能减小锁的粒度  效率比上面好点
 * 需要双重检查
 */
public class Singleton04 {
    //注意使用volatile关键字修饰
    private static volatile Singleton04 instance = null;
    private Singleton04() {
    }

    public static Singleton04 getInstance() {
        if (instance == null) {
            synchronized (Singleton04.class) {
                if (instance == null) {
                    instance = new Singleton04();
                }
            }

        }

        return instance;
    }
}
